<?php
/**
 * @file $HeadURL: imagecollectivecategoryDefine.php $
 * @author $Author: DongJC (dongjianchang@haibao.com) $
 * @date $Date: 2015-4-1 7:24:11 $
 * @brief  
 */
namespace haibao\www\business\imagecollective;

use \haibao\www\data\imagecollective\ImageCollectiveCategoryDefine as CategoryDefineData ;
use \haibao\frame\data\query\Condition ;
use \haibao\www\model\filter\imagecollective\ImageCollectiveCategoryDefine as CategoryDefineFilter;
use \haibao\www\business\BusinessException;
use \haibao\www\business\Base;

class ImageCollectiveCategoryDefine extends \haibao\cms\business\Base{
	
	public static function showImageCollectiveCategory(){
		$data = new CategoryDefineData();
		return $data->getCategoryTree();
	}
	
	public static function getAllCategorys(){
		$data = new CategoryDefineData();
		return $data->getAllCategorysDict();
	}
	
	public static function getAllChildrenCategory($categoryId){
		if(!$categoryId){
			throw new BusinessException('分类id不能为空');
		}
		$data = new CategoryDefineData();
		return $data->getAllChildrenCategory($categoryId);
	}
	
	
	public static function deleteCategoryDefine($categoryId){
		self::checkOperation(self::FUNC_CODE_EVENT_CATEGORY_DELETE);
		if(!$categoryId){
			throw new BusinessException('分类id不能为空');
		}
		$data = new CategoryDefineData();
		$data->deleteCategory($categoryId);
	
		self::saveOperateHistory(self::FUNC_CODE_EVENT_CATEGORY_DELETE, array('Id' =>$categoryId));
	}
	
	public static function updateCategory($model){
		self::checkOperation(self::FUNC_CODE_EVENT_CATEGORY_SAVE);
		
		if(!$model->ParentId && $model->ParentId!=0 ){
			throw new BusinessException('分类Id不能为空');
		}
		if(!$model->CategoryName ){
			throw new BusinessException('分类Id不能为空');
		}
		$data = new CategoryDefineData();
		$data->updateCategory($model);
		
		self::saveOperateHistory(self::FUNC_CODE_EVENT_CATEGORY_SAVE, $model->toArray(false));
	}
	
	public static function addCategory($model){
		self::checkOperation(self::FUNC_CODE_EVENT_CATEGORY_ADD);
		if(!isset($model->ParentId)){
			throw new BusinessException('请选择父级分类');
		}	
		if(!$model->CategoryName){
			throw new BusinessException('分类名不能为空');
		}
		if(!self::hasSameCategoryName($model->CategoryName,$model->ParentId,'')){
			throw new BusinessException('分类名称已存在',Base::DATA_ERROR_NAME_EXIST);
		}		
		$data = new CategoryDefineData();
		$data->addCategory($model);
		
		self::saveOperateHistory(self::FUNC_CODE_EVENT_CATEGORY_ADD, $model->toArray(false));
	}
	
	public static function hasSameCategoryName($categoryName,$parentId,$categoryId){
		if(!$categoryName){
			throw new BusinessException('分类名称不能为空',Base::DATA_EMPTY_NAME);
		}
		$categoryDefineFilter = new CategoryDefineFilter();
		if($categoryId){
			$categoryDefineFilter->where($categoryDefineFilter::CONDITION_FIELD_NAME_ID, Condition::CONDITION_NOTEQUAL, $categoryId);
		}
		$categoryDefineFilter->where($categoryDefineFilter::CONDITION_FIELD_NAME_PARENT_ID, Condition::CONDITION_EQUAL, $parentId);
		$categoryDefineFilter->where($categoryDefineFilter::CONDITION_FIELD_NAME_CATEGORY_NAME, Condition::CONDITION_EQUAL, $categoryName);
	
		$data = new CategoryDefineData();
		if($data->getAll($categoryDefineFilter)){
			return false;
		}else{
			return true;
		}
	}
	
	public static function getCategoryById($id){
		if(!$id){
			throw new BusinessException('分类id不能为空');
		}
		$data = new CategoryDefineData();
		$model = $data->getCategoryById($id);
		if (!$model){
			throw new BusinessException('分类不存在', self::DATA_EMPTY);
		}
	
		return $model;
	}
}